#include "pch.h"
#include "Application.h"

#include <logger/memorystream.hpp>
#include <services/scheduler.hpp>

#ifdef _DEBUG
	#include <mld/mld.hpp>
#endif




Application::Application(int argc, char *argv[])
	: QApplication(argc, argv)
	, m_provider(new services::provider)
{
	m_provider->registerService(services::service::ptr(new logger::memorystream(m_provider)));
	m_provider->registerService(services::service::ptr(new services::scheduler(m_provider)));
}
///////////////////////////////////////////////////////////////////////////////////////////////////

Application::~Application()
{}
///////////////////////////////////////////////////////////////////////////////////////////////////




void Application::onException(const std::exception &except)
{
	QMessageBox msgBox(activeWindow());
	msgBox.setWindowTitle(tr("An exception has been caught"));
	msgBox.setText(tr("An exception causes this application to shut down. I'm sorry for the inconvenience this may cause. Please press Ok to close the Application."));
	msgBox.setInformativeText(tr("The exception is most likely to be a mistake the programmer made. Informing him about the problem will improve the overall quality of this software."));
	msgBox.setDetailedText(QString(tr("Type: std::exception\nMessage: %1")).arg(except.what()));

	msgBox.setIconPixmap(QPixmap(":/ico/warning"));
	msgBox.addButton(QMessageBox::Ok);

	msgBox.exec();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void Application::onException(const boostext::exception& except)
{
	QMessageBox msgBox(activeWindow());
	msgBox.setWindowTitle(tr("An exception has been caught"));
	msgBox.setText(tr("An exception causes this application to shut down. I'm sorry for the inconvenience this may cause. Please press Ok to close the Application."));
	msgBox.setInformativeText(tr("The exception is most likely to be a mistake the programmer made. Informing him about the problem will improve the overall quality of this software."));
	msgBox.setDetailedText(QString(boost::diagnostic_information(except).c_str()));

	msgBox.setIconPixmap(QPixmap(":/ico/warning"));
	msgBox.addButton(QMessageBox::Ok);

	msgBox.exec();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

bool Application::notify(QObject* receiver, QEvent* event)
{
	try
	{
		return QApplication::notify(receiver, event);
	}
	catch(const std::exception &except)
	{
		onException(except);
		exit(-1);
		return false; //< dummy
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
